package org.daidai.abstrusewriting.homepage.hot.client;

import com.google.gson.Gson;

import org.daidai.abstrusewriting.homepage.hot.model.ADinformationData;
import org.daidai.abstrusewriting.homepage.hot.model.DiscoveryColumns;
import org.daidai.abstrusewriting.homepage.hot.model.EditorRecommendAlbums;
import org.daidai.abstrusewriting.homepage.hot.model.FocusImages;
import org.daidai.abstrusewriting.homepage.hot.model.Guess;
import org.daidai.abstrusewriting.homepage.hot.model.HotRecommends;
import org.daidai.abstrusewriting.homepage.hot.model.ProgremTracks;
import org.daidai.abstrusewriting.homepage.hot.model.RecommendDetail;
import org.daidai.abstrusewriting.homepage.hot.model.SpecialColumn;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;



/**
 * Created by Administrator on 2016/10/31.
 */

public class ClientApi {
    private static OkHttpUtilImp okUtil;

    static {
        okUtil = new OkHttpUtilImp();
    }

    public static void getFirstJsonBody(String url) {
        okUtil.doGetDataSync(url,new HttpCallBack() {
            @Override
            public void onSuccess(int code, byte[] data) {
                if (code == 200) {
                    try {
                        String s = new String(data, "UTF-8");
                        JSONObject allData = new JSONObject(s);
                        String msg = allData.optString("msg");
                        if ("成功".equals(msg)) {
                            JSONObject discoveryColumnsJson = allData.getJSONObject("discoveryColumns");
                            Gson gson = new Gson();
                            DiscoveryColumns discoveryColumns = gson.fromJson(discoveryColumnsJson.toString(), DiscoveryColumns.class);
                            EventBus.getDefault().post(discoveryColumns);
                            //TODO:传送数据
                            JSONObject editorRecommendAlbumsJson = allData.getJSONObject("editorRecommendAlbums");
                            EditorRecommendAlbums editorRecommendAlbums = gson.fromJson(editorRecommendAlbumsJson.toString(), EditorRecommendAlbums.class);
                            EventBus.getDefault().post(editorRecommendAlbums);
                            //TODO:传送数据
                            JSONObject focusImagesJson = allData.getJSONObject("focusImages");
                            FocusImages focusImages = gson.fromJson(focusImagesJson.toString(), FocusImages.class);
                            //TODO:传送数据
                            EventBus.getDefault().post(focusImages);
                            JSONObject specialColumnJson = allData.getJSONObject("specialColumn");
                            SpecialColumn specialColumn = gson.fromJson(specialColumnJson.toString(), SpecialColumn.class);
                            EventBus.getDefault().post(specialColumn);
                            //TODO:传送数据
                        }
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    public static void getSecondJsonBody(String url) {
        okUtil.doGetDataSync(url, new HttpCallBack() {
            @Override
            public void onSuccess(int code, byte[] data) {
                if (code == 200) {
                    try {
                        String s = new String(data, "UTF-8");
                        JSONObject allData = new JSONObject(s);
                        String msg = allData.optString("msg");
                        if ("成功".equals(msg)) {
                            Gson gson = new Gson();
                            JSONObject guessJson = allData.getJSONObject("guess");
                            Guess guess = gson.fromJson(guessJson.toString(), Guess.class);
                            //TODO:传送数据
                            EventBus.getDefault().post(guess);
                            JSONObject hotRecommendsJson = allData.getJSONObject("hotRecommends");
                            HotRecommends hotRecommends = gson.fromJson(hotRecommendsJson.toString(), HotRecommends.class);
                            EventBus.getDefault().post(hotRecommends);
                            //TODO:传送数据
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        }

    public static void getThirdJsonBody(String url){
        okUtil.doGetDataSync(url, new HttpCallBack() {
            @Override
            public void onSuccess(int code, byte[] data) {
                if (code==200) {
                    String s = null;
                    try {
                        s = new String(data,"UTF-8");
                        JSONObject allData = new JSONObject(s);
                        Gson gson = new Gson();
                        ADinformationData aDinformationData = gson.fromJson(allData.toString(), ADinformationData.class);
//
                        //TODO:传送数据
                        EventBus.getDefault().post(aDinformationData);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    public static void getRecommendDetail(String url){
        okUtil.doGetDataSync(url, new HttpCallBack() {
            @Override
            public void onSuccess(int code, byte[] data) {
                if (code==200) {
                    String s = null;
                    try {
                        s = new String(data, "UTF-8");
                        JSONObject allData = new JSONObject(s);
                        JSONObject dataJson = allData.getJSONObject("data");
                        Gson gson = new Gson();
                        RecommendDetail recommendDetail = gson.fromJson(dataJson.toString(), RecommendDetail.class);
                        EventBus.getDefault().post(recommendDetail);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }
        });
    }

    public static void getProgremDetail(String url){
        okUtil.doGetDataSync(url, new HttpCallBack() {
            @Override
            public void onSuccess(int code, byte[] data) {
                if (code==200) {
                    String s = null;
                    try {
                        s = new String(data, "UTF-8");
                        JSONObject allData = new JSONObject(s);
                        JSONObject dataJson = allData.getJSONObject("data");
                        JSONObject tracksJson = dataJson.getJSONObject("tracks");
                        Gson gson = new Gson();
                        ProgremTracks progremTracks = gson.fromJson(tracksJson.toString(), ProgremTracks.class);
                        EventBus.getDefault().post(progremTracks);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }
        });
    }
    }

